package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.MessageContacts;
import com.czy.owner.widget.RoundImageView;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by King on 2016/6/2 0002.
 */
public class MessageContactsAdapter  extends BaseRecyclerAdapter<MessageContacts,MessageContactsAdapter.MessageContactsHolder> implements RecyclerHeadersAdapter<RecyclerView.ViewHolder>{
    public MessageContactsAdapter(Context context, List<MessageContacts> data) {
        super(context, data);
    }
    @Override
    public MessageContactsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_message_contacts_recycler, parent, false);
        return new MessageContactsHolder(view);
    }
    @Override
    public void onBindViewHolder(MessageContactsHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));
    }


    @Override
    public long getHeaderId(int position) {
        return data.get(position).getSortLetters().charAt(0);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.recycler_contacts_header, parent, false);
        return new RecyclerView.ViewHolder(view){};
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        TextView textView = (TextView) holder.itemView.findViewById(R.id.tv_recycler_header);
        String showValue = String.valueOf(data.get(position).getSortLetters().charAt(0));
        textView.setText(showValue);
    }

    public int getPositionForSection(char section) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr = data.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;

    }

    public String[] getArraySortLetters() {
        if (data.size() == 0) {
            return null;
        } else {
            LinkedList<String> listSortLetters = new LinkedList<>();
            for (int i = 0; i < data.size(); i++) {
                if (i == 0 || !listSortLetters.getLast().equals(data.get(i).getSortLetters().toUpperCase())) {
                    listSortLetters.add(data.get(i).getSortLetters().toUpperCase());
                }
            }
            String[] arraySortLetters = new String[listSortLetters.size()];
            arraySortLetters = listSortLetters.toArray(arraySortLetters);
            return arraySortLetters;
        }
    }

    public class MessageContactsHolder extends BaseRecyclerAdapter.ViewHolder {
        private RoundImageView ivAvatar;
        private TextView tvTitle;
        private TextView tvContent;


        public MessageContactsHolder(View view) {
            super(view);
            ivAvatar = (RoundImageView)view.findViewById(R.id.iv_message_contacts_item_avatar);
            tvTitle = (TextView) view.findViewById(R.id.tv_message_contacts_item_title);
            tvContent = (TextView) view.findViewById(R.id.tv_message_contacts_item_content);
        }

        public void bindData(MessageContacts mMembers) {
            if(mMembers.getUserLogo()!=null && !mMembers.getUserLogo().isEmpty()){
                ivAvatar.setTag(mMembers.getUserLogo());
                x.image().bind(ivAvatar, mMembers.getUserLogo(), new Callback.CommonCallback<Drawable>() {
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        ivAvatar.setImageResource(R.mipmap.icon_connect_portrait);
                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {
                        ivAvatar.setImageResource(R.mipmap.icon_connect_portrait);
                    }

                    @Override
                    public void onSuccess(Drawable result) {
                    }
                });
            }else{
                ivAvatar.setImageResource(R.mipmap.icon_connect_portrait);
            }
            tvTitle.setText(mMembers.getEmployeesName()+"-"+mMembers.getGroupName());
            tvContent.setText(mMembers.getContact());
        }
    }


}
